using System;
using System.Collections.Generic;
using System.Text;
using System.Resources;
using Microsoft.Win32;
using System.Reflection;

namespace ClassBuilderWizard
{
    /// <summary>
    /// Responsible for retrieving and updating information in the Registry
    /// </summary>
    public class RegistryHelper
    {
        private RegistryKey LastConnection;
        private RegistryKey _ShowPluginDetails;
        //private RegistryKey _InstallPath;
        private string _InstallPath;

        public RegistryHelper()
        {
            Assembly assm = Assembly.GetExecutingAssembly();
            AssemblyCompanyAttribute comp = (AssemblyCompanyAttribute)assm.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false)[0];
            AssemblyProductAttribute prod = (AssemblyProductAttribute)assm.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0];
            this.LastConnection = Registry.CurrentUser.CreateSubKey(@"Software\" + comp.Company + @"\" + prod.Product);
            this._ShowPluginDetails = Registry.CurrentUser.CreateSubKey(string.Format(@"Software\{0}\{1}", comp.Company, prod.Product));
            //this._InstallPath = Registry.LocalMachine.CreateSubKey(string.Format(@"Software\{0}\{1}", comp.Company, prod.Product));
            this._InstallPath = Registry.LocalMachine.OpenSubKey(string.Format(@"Software\{0}\{1}", comp.Company, prod.Product)).GetValue("InstallPath").ToString();
        }

        public string GetInstallPath()
        {
            //string val = (string)this._InstallPath.GetValue("InstallPath", string.Empty);
            return this._InstallPath;
        }
        public string GetLastConnection()
        {
            //get the database connection string
            string val = (string)LastConnection.GetValue("LastDbConn", string.Empty);

            return val;
        }

        public void SaveLastConnection(string conn)
        {
            //save the database connection string
            LastConnection.SetValue("LastDbConn", conn);
        }

        public bool GetShowPluginDetails()
        {
            //get the last plugin view mode
            bool val = (bool)this._ShowPluginDetails.GetValue("ShowPluginDetails", true);

            return val;
        }

        public void SaveShowPluginDetails(bool showDetails)
        {
            this._ShowPluginDetails.SetValue("ShowPluginDetails", showDetails);
        }

        public static string GetVSInstallPathFromRegistry(string registryRoot)
        {
            string result = string.Empty;

            //result = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\VisualStudio\\8.0").GetValue("InstallDir") as string;
            result = Registry.LocalMachine.OpenSubKey(registryRoot).GetValue("InstallDir") as string;
            result += "..\\";

            return result;
        }

    }

    
}
